package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "数据字典管理接口")
@Slf4j
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {
    @Autowired
    private DictService dictService;

    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("/findChildData/{id}")
    public Result<List<Dict>> findChildData(@PathVariable("id") Long id) {

        List<Dict> dictList = dictService.findChildData(id);

        return Result.ok(dictList);
    }

    @ApiOperation(value = "根据dictode查询下层节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result<List<Dict>> findChildDataByDictCode(@PathVariable("dictCode") String dictCode) {

        List<Dict> dictList = dictService.findDictListByDictCode(dictCode);

        return Result.ok(dictList);
    }

    @ApiOperation(value = "excel的导出")
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response) {
        // 数据导出
        dictService.exportData(response);
    }

    @ApiOperation(value = "excel的导入")
    @PostMapping("/importData")
    public Result importData(MultipartFile file) {
        // 数据上传
        dictService.importData(file);
        return Result.ok();
    }

    // 根据dictCode和value查询 | 提供两个api接口，如省市区不需要上级编码，医院等级需要上级编码
    @ApiOperation("据dictCode和value查询")
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable("dictCode") String dictCode,
                                  @PathVariable("value") String value) {
        String dictName = dictService.getNameByParentDictCodeAndValue(dictCode, value);
        return dictName;
    }

    // 根据value查询
    @ApiOperation("据value查询")
    @GetMapping("getName/{value}")
    public String getName(@PathVariable("value") String value) {
        String dictName = dictService.getNameByParentDictCodeAndValue("", value);
        return dictName;
    }

}
